﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using HostAssistantCore;
namespace HostAssistant
{
    public partial class Form1 : U1Form
    {
        public Form1()
        {
            InitializeComponent();
            foreach (DevExpress.XtraNavBar.NavBarItem item in navBarControl1.Items)
            {
                item.LinkClicked += new DevExpress.XtraNavBar.NavBarLinkEventHandler(item_LinkClicked);
            }

        }

        #region 菜单方法

        public void op(string s)
        {
            var view = CreateView(s);
            OpenTab(view, "");
        }

        void item_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
             if (e.Link.Item.Tag.UString() == string.Empty)
            {
                return;
            }
       
            var view = CreateView(e.Link.Item.Tag.UString());
            OpenTab(view, e.Link.Caption);

        }


          U1UserControl CreateView(string tag)
        {
            U1UserControl instanceView=new U1UserControl ();
            if (tag == "站点列表")
                instanceView = new sitelist();
            else
                  if (tag == "数据库列表")
                instanceView = new dblist();
            else
                  if (tag == "系统设置")
                instanceView = new setting();

            else
                  if (tag == "FTP列表")
                instanceView = new ftp();

            else   if (tag == "首页")
                instanceView = new index();




            return instanceView;

        }

        void xTabMain_CloseButtonClick(object sender, EventArgs e)
        {
            xTabMain.TabPages.Remove(xTabMain.SelectedTabPage);
        }


        private DevExpress.XtraTab.XtraTabPage OpenTab(U1UserControl control, string caption)
        {
            string name = "main";
            foreach (XtraTabPage p in xTabMain.TabPages)
            {
                if (p.Text == name)
                {
                    xTabMain.TabPages.Remove(p);
                    p.Dispose();
                    break;
                }

             
            }

         


            DevExpress.XtraTab.XtraTabPage page = new DevExpress.XtraTab.XtraTabPage();
            page.Text = name;
            control.Dock = DockStyle.Fill;
            page.Controls.Add(control);
            this.xTabMain.TabPages.Add(page);
            xTabMain.SelectedTabPage = page;
            return page;
        }


        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {

            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (process.MainModule.FileName
                    == current.MainModule.FileName)
                    {
                       

                        DevExpress.XtraEditors.XtraMessageBox.Show("程序已经运行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                        System.Environment.Exit(0);
                        return;
                    }
                }
            }






            var view = CreateView("首页");
            OpenTab(view, "首页");


        }

        #region 返回首页

        private void labelControl1_Click(object sender, EventArgs e)
        {
            var view = CreateView("首页");
            OpenTab(view, "首页");
        }

        #endregion

    }
}
